<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="SimpleSpring开发-业务系统的构建[自研框架的起源](2), 胡言乱语">
    <meta name="description" content="MySimpleSpring业务系统架子的构建【自研框架的起源】
概述:
为了进一步明确我们自研框架的作用, 那我们就得用原生的脱离了Spring框架的JavaWeb方法去实现一个业务系统。
这样依赖，之后我们的自研框架就有了更多的出发点和">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    

    <title>SimpleSpring开发-业务系统的构建[自研框架的起源](2) | 胡言乱语</title>
    <link rel="icon" type="image/x-icon, image/vnd.microsoft.icon" href="/myblog/favicon.ico">

    <link rel="stylesheet" type="text/css" href="/myblog/libs/awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="/myblog/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/myblog/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/myblog/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/myblog/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/myblog/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/myblog/css/my.css">

    <script src="/myblog/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 6.2.0"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/myblog/" class="waves-effect waves-light">
                    
                    <img src="/myblog/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">胡言乱语</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/myblog/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/myblog/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/myblog/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/myblog/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/myblog/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/myblog/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">胡言乱语</div>
        <div class="logo-desc">
            
            Never really desperate, only the lost of the soul.
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/myblog/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/myblog/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/myblog/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/myblog/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/myblog/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/myblog/medias/featureimages/20.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">SimpleSpring开发-业务系统的构建[自研框架的起源](2)</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/myblog/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/myblog/tags/%E5%90%8E%E7%AB%AF/">
                                <span class="chip bg-color">后端</span>
                            </a>
                        
                            <a href="/myblog/tags/%E9%80%A0%E8%BD%AE%E5%AD%90/">
                                <span class="chip bg-color">造轮子</span>
                            </a>
                        
                            <a href="/myblog/tags/Spring/">
                                <span class="chip bg-color">Spring</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/myblog/categories/SimpleSpring%E5%BC%80%E5%8F%91%E4%B8%93%E6%A0%8F/" class="post-category">
                                SimpleSpring开发专栏
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2022-06-04
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2022-06-04
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    2.9k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    11 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/myblog/libs/prism/prism.min.css">
        

        
        <!-- 代码块折行 -->
        <style type="text/css">
            code[class*="language-"], pre[class*="language-"] { white-space: pre-wrap !important; }
        </style>
        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h1 id="MySimpleSpring"><a href="#MySimpleSpring" class="headerlink" title="MySimpleSpring"></a>MySimpleSpring</h1><h2 id="业务系统架子的构建【自研框架的起源】"><a href="#业务系统架子的构建【自研框架的起源】" class="headerlink" title="业务系统架子的构建【自研框架的起源】"></a>业务系统架子的构建【自研框架的起源】</h2><ul>
<li><p>概述:</p>
<p>为了进一步明确我们自研框架的作用, 那我们就得用原生的脱离了Spring框架的JavaWeb方法去实现一个业务系统。</p>
<p>这样依赖，之后我们的自研框架就有了更多的出发点和落足点。</p>
</li>
<li><p>业务系统的需求分析与基本设计：</p>
<ul>
<li><p>考虑到这只是一个启发式的小系统，所以我们无需太完善，比如链接数据库等操作可以直接省略，</p>
<p>我们只需要关心业务相关的<strong>模块之间的交互 以及 类的管理</strong></p>
</li>
<li><p>需求：</p>
<p><img src="/myblog/%5CmdImage%5Cimage-20220527224145765.png" alt="image-20220527224145765"></p>
</li>
<li><p>由此需求，可以分析出只需要两个表：一个是头条表，一个是商铺表，同时也对应两个实体类。</p>
<p>由于我们此处不会去关心数据库层面的，所以只需要设计两个实体类即可：</p>
<p><img src="/myblog/%5CmdImage%5Cimage-20220527224310289.png" alt="image-20220527224310289"></p>
</li>
<li><p>同时，因为是业务系统，所以我们尽量采取MVC架构来进行开发（其实更多指的是三层架构）：</p>
<ul>
<li>Controller： 接收请求，并调用service层进行业务处理，并响应给前端</li>
<li>Service：业务处理</li>
<li>Dao：持久层的相关操作</li>
<li>Entity：实体类，一般对应数据库表</li>
</ul>
</li>
</ul>
</li>
<li><p>代码实现：</p>
<ul>
<li><p>pom.xml文件中引入 slf4j 和 lombok依赖:</p>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token comment">&lt;!-- 日志依赖 --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.slf4j<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>slf4j-log4j12<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.7.28<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
<span class="token comment">&lt;!-- lombok依赖 --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.projectlombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>lombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.18.10<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</li>
<li><p>entity包下创建两个实体类:</p>
<p>店铺实体类:</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">/**
 * 店铺实体类
 */</span>
<span class="token annotation punctuation">@Data</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ShopCategory</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">private</span> <span class="token class-name">Long</span> shopCategoryId<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> shopCategoryName<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> shopCategoryDesc<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> shopCategoryImg<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> priority<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Date</span> createTime<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Date</span> lastEditTime<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">ShopCategory</span> parent<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>头条实体类:</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">/**
 * 头条实体类
 */</span>
<span class="token annotation punctuation">@Data</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HeadLine</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">private</span> <span class="token class-name">Long</span> lineId<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> lineName<span class="token punctuation">;</span>
    <span class="token keyword">private</span>  <span class="token class-name">String</span> lineLink<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> lineImg<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> priority<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> enableStatus<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Date</span> createTime<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Date</span> lastEditTime<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</li>
<li><p>dto包下,创建用于响应给前端的响应信息的通用类:</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Data</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">></span></span> <span class="token punctuation">&#123;</span>
    <span class="token comment">//本次请求结果的状态码，200表示成功</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> code<span class="token punctuation">;</span>
    <span class="token comment">//本次请求结果的详情</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> msg<span class="token punctuation">;</span>
    <span class="token comment">//本次请求返回的结果集</span>
    <span class="token keyword">private</span> <span class="token class-name">T</span> data<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</li>
<li><p>dao层: 由于本业务系统不考虑数据库,所以为了省事,就不考虑dao层了</p>
</li>
<li><p>service包下,对应着业务需求, 创建业务处理对应的Service:</p>
<p>头条相关业务处理的Service:</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">HeadLineService</span> <span class="token punctuation">&#123;</span>
    <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Boolean</span><span class="token punctuation">></span></span> <span class="token function">addHeadLine</span><span class="token punctuation">(</span><span class="token class-name">HeadLine</span> headLine<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Boolean</span><span class="token punctuation">></span></span> <span class="token function">removeHeadLine</span><span class="token punctuation">(</span><span class="token keyword">int</span> headLineId<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Boolean</span><span class="token punctuation">></span></span> <span class="token function">modifyHeadLine</span><span class="token punctuation">(</span><span class="token class-name">HeadLine</span> headLine<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">HeadLine</span><span class="token punctuation">></span></span> <span class="token function">queryHeadLineById</span><span class="token punctuation">(</span><span class="token keyword">int</span> headLineId<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">HeadLine</span><span class="token punctuation">></span><span class="token punctuation">></span></span><span class="token function">queryHeadLine</span><span class="token punctuation">(</span><span class="token class-name">HeadLine</span> headLineCondition<span class="token punctuation">,</span> <span class="token keyword">int</span> pageIndex<span class="token punctuation">,</span> <span class="token keyword">int</span> pageSize<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>店铺相关业务处理的Service:</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">ShopCategoryService</span> <span class="token punctuation">&#123;</span>
    <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Boolean</span><span class="token punctuation">></span></span> <span class="token function">addShopCategory</span><span class="token punctuation">(</span><span class="token class-name">ShopCategory</span> shopCategory<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Boolean</span><span class="token punctuation">></span></span> <span class="token function">removeShopCategory</span><span class="token punctuation">(</span><span class="token keyword">int</span> shopCategoryId<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Boolean</span><span class="token punctuation">></span></span> <span class="token function">modifyShopCategory</span><span class="token punctuation">(</span><span class="token class-name">ShopCategory</span> shopCategory<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">ShopCategory</span><span class="token punctuation">></span></span> <span class="token function">queryShopCategoryById</span><span class="token punctuation">(</span><span class="token keyword">int</span> shopCategoryId<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">ShopCategory</span><span class="token punctuation">></span><span class="token punctuation">></span></span> <span class="token function">queryShopCategory</span><span class="token punctuation">(</span><span class="token class-name">ShopCategory</span> shopCategoryCondition<span class="token punctuation">,</span> <span class="token keyword">int</span> pageIndex<span class="token punctuation">,</span> <span class="token keyword">int</span> pageSize<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>合并两个Service,用于实现返回首页相关信息的Service:</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">HeadLineShopCategoryCombineService</span> <span class="token punctuation">&#123;</span>
    <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">MainPageInfoDTO</span><span class="token punctuation">></span></span> <span class="token function">getMainPageInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>再次强调, 由于本项目只关心模块之间的联系以及类的处理, 所以并不会具体实现这些方法, 所以这里就不一一展示对应的impl实现类了,</p>
<p>这里只贴其中一个实现类:</p>
<p>HeadLineShopCategoryCombineService的实现类:</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HeadLineShopCategoryCombineServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">HeadLineShopCategoryCombineService</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">private</span> <span class="token class-name">HeadLineService</span> headLineService<span class="token punctuation">;</span> <span class="token comment">// 头条Service</span>
    <span class="token keyword">private</span> <span class="token class-name">ShopCategoryService</span> shopCategoryService<span class="token punctuation">;</span><span class="token comment">// 店铺Service</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">MainPageInfoDTO</span><span class="token punctuation">></span></span> <span class="token function">getMainPageInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// 1.获取头条列表</span>
        <span class="token class-name">HeadLine</span> headLineCondition <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HeadLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        headLineCondition<span class="token punctuation">.</span><span class="token function">setEnableStatus</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">HeadLine</span><span class="token punctuation">></span><span class="token punctuation">></span></span> <span class="token class-name">HeadLineResult</span> <span class="token operator">=</span> headLineService<span class="token punctuation">.</span><span class="token function">queryHeadLine</span><span class="token punctuation">(</span>headLineCondition<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 2.获取店铺类别列表</span>
        <span class="token class-name">ShopCategory</span> shopCategoryCondition <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ShopCategory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">ShopCategory</span><span class="token punctuation">></span><span class="token punctuation">></span></span> shopCategoryResult <span class="token operator">=</span>shopCategoryService<span class="token punctuation">.</span><span class="token function">queryShopCategory</span><span class="token punctuation">(</span>shopCategoryCondition<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 3.合并两者并返回</span>
        <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">MainPageInfoDTO</span><span class="token punctuation">></span></span> result <span class="token operator">=</span> <span class="token function">mergeMainPageInfoResult</span><span class="token punctuation">(</span><span class="token class-name">HeadLineResult</span><span class="token punctuation">,</span> shopCategoryResult<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> result<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token keyword">private</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">MainPageInfoDTO</span><span class="token punctuation">></span></span> <span class="token function">mergeMainPageInfoResult</span><span class="token punctuation">(</span><span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">HeadLine</span><span class="token punctuation">></span><span class="token punctuation">></span></span> headLineResult<span class="token punctuation">,</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">ShopCategory</span><span class="token punctuation">></span><span class="token punctuation">></span></span> shopCategoryResult<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">return</span>  <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>**此处其实就能发现, 我们出现了Service之间的包含关系,即: 一个类有其他类作为其成员变量 **</p>
<p>**此时我们肯定要对他们进行初始化的, 否则就是null, 无法使用,这里由于这个系统的业务简单, 嵌套关系并不深, 只不过是new一下就能解决 **</p>
<p><strong>如果在稍微大型的项目里面,一个Service用到了多个Service, 一个Service下面可能还有多个dao成员, 那么此时手动去初始化就会变得非常非常复杂</strong></p>
<p>**此刻我们是否需要一个可以帮我们实现自动化注入(初始化), 来帮我们自动管理类与类之间关系 的工具呢? **</p>
</li>
<li><p>controller层: </p>
<p><strong>此时有一个特别需要注意的点:</strong></p>
<p><strong>之前开发我们一直用Spring封装好的Controller类, 我们可以随意在Controller里面通过@RequestMapping给各个controller方法指定其对应的请求方法和请求路径, 这样就很容易实现 一个Controller类对应其模块的所有请求 — 例如: StudentController可以在这个类里面一下子设置所有的请求操作: 请求学生列表,请求单个学生信息, 修改学生信息等等, 我们只要设置好@RequestMapping就可以随意在一个Controller类里面放入各种请求处理</strong></p>
<ul>
<li><p>**但是,此时我们是原生Servlet, 如果照葫芦画瓢, 我们直接用Servlet充当Controller类 来 处理对应的一个模块 行不行? **</p>
<p><strong>答案是不行!  因为一个Servlet只能对应一个路径, 一个路径下只有 doGet&#x2F;doPost&#x2F;doPut…的方法区别. 我们无法实现像Controller类那样的随便往里面放入请求处理, 例如: 学生模块需要两个Get方法: 一个是请求学生列表, 一个是请求单个学生信息, 这两个方法, 如果用一个StudentServlet来处理, 很明显是不能处理的, 因为StudentServlet只能有一个路径, 且只有一个doGet方法, 所以顶多只能实现其中的一个需求, 所以: 直接用Servlet来处理一个模块是行不通的</strong></p>
</li>
<li><p>**但是Servlet有doGet,doPost,doPut等方法, 不是刚好对应了一个实体的增删改查操作吗? 那我们改一下思路: 一个Servlet对应处理一个实体类, 行不行? **</p>
<p><strong>答案也是不行的! 首先当一个项目大起来, 可能会有一堆表, 按照一个表对应一个实体的一般原则, 那么就会有一堆的实体类, 那么就会有一堆的Servlet, 这样肯定是不行的, 其次还是回到刚刚那个需求, 我们如果要请求学生列表怎么办呢? 这种思想也是很难实现的</strong></p>
</li>
<li><p><strong>那Servlet对应一个页面的请求处理行不行? 也不行, 还是刚刚那个例子: 一个页面上可能会要求查询单个学生, 也会要求查询所有学生列表, 那么这种情况Servlet也是无法处理的</strong></p>
</li>
<li><p>(写到这的时候, 我突然兴起, 去看了以前大二学JavaWeb时期的项目, 居然是: 一个Servlet对应一个请求然后对应一个Service!!??, 我差点蚌埠住了, 包括我刚刚想了这么多种方案选型, 都根本想不到这么离谱的做法哈哈哈, 只能说过了一年了, 我果然是在进步的哈哈哈 )</p>
</li>
</ul>
<p>总结上述这几种Servlet无法应对的情况, 归根结底就两个原因:</p>
<ul>
<li>Servlet太多不好 </li>
<li>Servlet只能对应一个路径, 且只有一个doGet,一个doPost…无法实现Controller类的那种模块化统一请求处理</li>
</ul>
<p>那么, 我们应该怎么样实现 <strong>Controller类的那种模块化统一请求处理</strong> 呢?</p>
<p>**既然都是Servlet的错, 那我们干脆不用Servlet不就行了吗? **</p>
<p><strong>羊毛出在羊身上, 我们参考一下SpringMVC的做法 – 用一个Servlet类DispatcherServlet类, 然后用多个普通Java类作为Controller类, 由DispatcherServlet类接收所有的请求, 根据请求的路径以及方法, 对应着调用普通Controller类来进行处理。</strong></p>
<p><strong>这样一来,我们尽管往Controller类里面添加方法,因为他没有了Servlet的限制, 这样就能实现一个模块的请求全部放在一个类里面处理了, 同时因为Controller类只是一个普普通通的Java类, 那么就算添加再多Controller类, 也不会有Servlet太多的顾虑</strong></p>
<p>我们来定义几个Controller类:</p>
<p>首页模块对应的Controller类:</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MainPageController</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">private</span> <span class="token class-name">HeadLineShopCategoryCombineService</span> headLineShopCategoryCombineService<span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">MainPageInfoDTO</span><span class="token punctuation">></span></span> <span class="token function">getMainPageInfo</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token keyword">return</span> headLineShopCategoryCombineService<span class="token punctuation">.</span><span class="token function">getMainPageInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">throwException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span><span class="token string">"抛出异常测试"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>头条操作模块对应的:</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HeadLineOperationController</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">private</span> <span class="token class-name">HeadLineService</span> headLineService<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Boolean</span><span class="token punctuation">></span></span> <span class="token function">addHeadLine</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span><span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//TODO:参数校验以及请求参数转化</span>
        <span class="token class-name">HeadLine</span> headLine <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HeadLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Boolean</span><span class="token punctuation">></span></span> result <span class="token operator">=</span> headLineService<span class="token punctuation">.</span><span class="token function">addHeadLine</span><span class="token punctuation">(</span>headLine<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> result<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">removeHeadLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"删除HeadLine"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Boolean</span><span class="token punctuation">></span></span> <span class="token function">modifyHeadLine</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//TODO:参数校验以及请求参数转化</span>
        <span class="token keyword">return</span> headLineService<span class="token punctuation">.</span><span class="token function">modifyHeadLine</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HeadLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">HeadLine</span><span class="token punctuation">></span></span> <span class="token function">queryHeadLineById</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//TODO:参数校验以及请求参数转化</span>
        <span class="token keyword">return</span> headLineService<span class="token punctuation">.</span><span class="token function">queryHeadLineById</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">HeadLine</span><span class="token punctuation">></span><span class="token punctuation">></span></span><span class="token function">queryHeadLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token keyword">return</span> headLineService<span class="token punctuation">.</span><span class="token function">queryHeadLine</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>店铺操作模块对应的:</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ShopCategoryOperationController</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">private</span> <span class="token class-name">ShopCategoryService</span> shopCategoryService<span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Boolean</span><span class="token punctuation">></span></span> <span class="token function">addShopCategory</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//TODO:参数校验以及请求参数转化</span>
        <span class="token keyword">return</span> shopCategoryService<span class="token punctuation">.</span><span class="token function">addShopCategory</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ShopCategory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Boolean</span><span class="token punctuation">></span></span> <span class="token function">removeShopCategory</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//TODO:参数校验以及请求参数转化</span>
        <span class="token keyword">return</span> shopCategoryService<span class="token punctuation">.</span><span class="token function">removeShopCategory</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Boolean</span><span class="token punctuation">></span></span> <span class="token function">modifyShopCategory</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//TODO:参数校验以及请求参数转化</span>
        <span class="token keyword">return</span> shopCategoryService<span class="token punctuation">.</span><span class="token function">modifyShopCategory</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ShopCategory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">ShopCategory</span><span class="token punctuation">></span></span> <span class="token function">queryShopCategoryById</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//TODO:参数校验以及请求参数转化</span>
        <span class="token keyword">return</span> shopCategoryService<span class="token punctuation">.</span><span class="token function">queryShopCategoryById</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">ShopCategory</span><span class="token punctuation">></span><span class="token punctuation">></span></span> <span class="token function">queryShopCategory</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//TODO:参数校验以及请求参数转化</span>
        <span class="token keyword">return</span> shopCategoryService<span class="token punctuation">.</span><span class="token function">queryShopCategory</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><strong>DispatcherServlet: 接收所有的请求, 然后根据请求路径以及请求方法, 调用对应的Controller类中的方法进行处理</strong></p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">/**
 * 请求分发器
 */</span>
<span class="token annotation punctuation">@WebServlet</span><span class="token punctuation">(</span><span class="token string">"/"</span><span class="token punctuation">)</span> <span class="token comment">// 接收所有的请求</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DispatcherServlet</span> <span class="token keyword">extends</span> <span class="token class-name">HttpServlet</span> <span class="token punctuation">&#123;</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// 1) 获得请求路径以及请求方法</span>
        <span class="token class-name">String</span> requestPath <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">getServletPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> requestMethod <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">getMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 2) 根据请求路径以及请求方法, 调用对应的Controller类进行</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token string">"frontend/getmainpageinfo"</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>requestPath<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token string">"GET"</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>requestMethod<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">new</span> <span class="token class-name">MainPageController</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getMainPageInfo</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span>resp<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token string">"superadmin/addheadline"</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>requestPath<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token string">"POST"</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>requestMethod<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">new</span> <span class="token class-name">HeadLineOperationController</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">addHeadLine</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span>resp<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><strong>此时我们发现, 上面的代码都是写死的! 当项目大起来, 会有以下缺点:</strong></p>
<ul>
<li><strong>每一次新增一个模块对应的Controller类时, 我们都必须得手动去DispatcherServlet的if-else链上面添加一个判断, 长期下去, 非常不利于维护与扩展</strong></li>
<li><strong>Controller中也包含了Service类成员变量, 这个问题在service层时就探讨过了, 总之就是手动初始化(注入)会很麻烦</strong></li>
<li><strong>Controller过于原生, 我们发现Controller里面传入的都是req,resp, 我们无论是对参数的获取还是响应参数, 都必须使用最原生的api手动去实现, 非常繁琐, 浪费精力</strong></li>
</ul>
<p>**我们是否需要一个可以直接把新增的Controller自动注册到DispatcherServlet中的工具呢? 是否需要一个自动从req获取参数, 同时更方便我们响应结果的工具呢? **</p>
</li>
</ul>
</li>
<li><p>从业务系统的开发中发现的问题总结:</p>
<ul>
<li><p>**我们是否需要一个可以帮我们实现自动化注入(初始化), 来帮我们自动管理类与类之间关系 的工具呢? **</p>
</li>
<li><p><strong>我们是否需要一个可以直接把新增的Controller自动注册到DispatcherServlet中的工具呢?</strong></p>
</li>
<li><p>**我们的Controller类是否需要一个自动从req获取参数, 同时更方便我们响应结果的工具呢? **</p>
<p>……</p>
</li>
</ul>
<p><strong>我们的自研框架, 能不能实现上述以及更多的需求, 使得我们的项目开发更简单呢?</strong></p>
</li>
</ul>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/myblog/about" rel="external nofollow noreferrer">Lin MingMing</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://mlming.gitee.io/myblog/myblog/2022/06/04/simplespring-kai-fa-ye-wu-xi-tong-de-gou-jian-zi-yan-kuang-jia-de-qi-yuan-2/">https://mlming.gitee.io/myblog/myblog/2022/06/04/simplespring-kai-fa-ye-wu-xi-tong-de-gou-jian-zi-yan-kuang-jia-de-qi-yuan-2/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/myblog/about" target="_blank">Lin MingMing</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/myblog/tags/%E5%90%8E%E7%AB%AF/">
                                    <span class="chip bg-color">后端</span>
                                </a>
                            
                                <a href="/myblog/tags/%E9%80%A0%E8%BD%AE%E5%AD%90/">
                                    <span class="chip bg-color">造轮子</span>
                                </a>
                            
                                <a href="/myblog/tags/Spring/">
                                    <span class="chip bg-color">Spring</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/myblog/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/myblog/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/myblog/2022/06/04/zuo-ti-xin-de-fang-fa-lun-shi-da-pai-xu-2/">
                    <div class="card-image">
                        
                        
                        <img src="/myblog/medias/featureimages/6.jpg" class="responsive-img" alt="做题心得-方法论-十大排序(2)">
                        
                        <span class="card-title">做题心得-方法论-十大排序(2)</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2022-06-04
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/myblog/categories/%E5%81%9A%E9%A2%98%E5%BF%83%E5%BE%97%E4%B8%93%E6%A0%8F/" class="post-category">
                                    做题心得专栏
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/myblog/tags/%E7%AE%97%E6%B3%95/">
                        <span class="chip bg-color">算法</span>
                    </a>
                    
                    <a href="/myblog/tags/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/">
                        <span class="chip bg-color">排序算法</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/myblog/2022/06/04/simplespring-kai-fa-zheng-ti-gan-zhi-yu-chu-shi-hua-1/">
                    <div class="card-image">
                        
                        
                        <img src="/myblog/medias/featureimages/12.jpg" class="responsive-img" alt="SimpleSpring开发-整体感知与初始化(1)">
                        
                        <span class="card-title">SimpleSpring开发-整体感知与初始化(1)</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2022-06-04
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/myblog/categories/SimpleSpring%E5%BC%80%E5%8F%91%E4%B8%93%E6%A0%8F/" class="post-category">
                                    SimpleSpring开发专栏
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/myblog/tags/%E5%90%8E%E7%AB%AF/">
                        <span class="chip bg-color">后端</span>
                    </a>
                    
                    <a href="/myblog/tags/%E9%80%A0%E8%BD%AE%E5%AD%90/">
                        <span class="chip bg-color">造轮子</span>
                    </a>
                    
                    <a href="/myblog/tags/Spring/">
                        <span class="chip bg-color">Spring</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/myblog/libs/codeBlock/codeBlockFuction.js"></script>


  <!-- 是否加载使用自带的 prismjs. -->
  <script type="text/javascript" src="/myblog/libs/prism/prism.min.js"></script>


<!-- 代码语言 -->

<script type="text/javascript" src="/myblog/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/myblog/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/myblog/libs/codeBlock/codeShrink.js"></script>



    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/myblog/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    

    <div class="container row center-align"
         style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2022</span>
            
            <a href="/myblog/about" target="_blank">Lin MingMing</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
                &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                        class="white-color">31.5k</span>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://gitee.com/mmwindy" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:1139380762@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1139380762" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 1139380762" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/myblog/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/myblog/libs/materialize/materialize.min.js"></script>
    <script src="/myblog/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/myblog/libs/aos/aos.js"></script>
    <script src="/myblog/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/myblog/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/myblog/js/matery.js"></script>

    

    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
     
        <script type="text/javascript">
            // 只在桌面版网页启用特效
            var windowWidth = $(window).width();
            if (windowWidth > 768) {
                document.write('<script type="text/javascript" src="/myblog/libs/others/star.js"><\/script>');
            }
        </script>
    

    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/myblog/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/myblog/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/myblog/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
